//
//  UIButton+LLNW.m
//  LLFoundation
//
//  Created by my_liu on 2016/11/14.
//  Copyright © 2016年 . All rights reserved.
//

#import "UIButton+LLNW.h"
#import "UIButton+AFNetworking.h"
#import "LLNWHTTPFileHandler.h"

@implementation UIButton (LLNW)
- (void)llnw_setImageForState:(UIControlState)state withURL:(NSURL *)url
{
    UIImage * image = [[LLNWHTTPFileHandler shareInstance] getImageWithUrl:url];
    
    if(image) {
        [self setImage:image forState:state];
        
        return;
    }
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:0];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    
    __weak UIButton * weakSelf = self;
    
    [self setImageForState:state withURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        
        [[LLNWHTTPFileHandler shareInstance] saveImage:image withUrl:url];
        
        [weakSelf setImage:image forState:state];
        
    } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
        
    }];
    
    
    
}

- (void)llnw_setImageForState:(UIControlState)state withURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage
{
    
    UIImage * image = [[LLNWHTTPFileHandler shareInstance] getImageWithUrl:url];
    
    if(image){
        
        [self setImage:image forState:state];
        
        return;
    }
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:0];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    
    
    __weak UIButton * weakSelf = self;
    
    [self setImageForState:state withURLRequest:request placeholderImage:placeholderImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        
        [[LLNWHTTPFileHandler shareInstance] saveImage:image withUrl:url];
        
        [weakSelf setImage:image forState:state];
        
    } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
        
    }];
}

- (void)llnw_setBackgroundImageForState:(UIControlState)state withURL:(NSURL *)url
{
    UIImage * image = [[LLNWHTTPFileHandler shareInstance] getImageWithUrl:url];
    
    if(image){
        
        [self setBackgroundImage:image forState:state];
        
        return;
    }
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:0];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    
    
    __weak UIButton * weakSelf = self;
    
    [self setBackgroundImageForState:state withURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        
        [[LLNWHTTPFileHandler shareInstance] saveImage:image withUrl:url];
        
        [weakSelf setBackgroundImage:image forState:state];
        
        
    } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
        
    }];
}

- (void)llnw_setBackgroundImageForState:(UIControlState)state withURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage
{
    
    UIImage * image = [[LLNWHTTPFileHandler shareInstance] getImageWithUrl:url];
    
    if(image){
        
        [self setBackgroundImage:image forState:state];
        
        return;
    }
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:0];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    
    
    __weak UIButton * weakSelf = self;
    
    [self setBackgroundImageForState:state withURLRequest:request placeholderImage:placeholderImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        
        [[LLNWHTTPFileHandler shareInstance] saveImage:image withUrl:url];
        
        [weakSelf setBackgroundImage:image forState:state];
        
    } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
        
    }];
}

- (void)llnw_cancelImageRequestOperationForState:(UIControlState)state
{
    [self cancelImageDownloadTaskForState:state];
}

- (void)llnw_cancelBackgroundImageRequestOperationForState:(UIControlState)state
{
    [self cancelBackgroundImageDownloadTaskForState:state];
}

@end
